# -*- coding: utf-8 -*-
# created on 2016/6/28
from sympy import S
from mathsolver.functions.base import BaseFunction, BaseIneq, base_gen
from mathsolver.functions.sympy_utils import safe_degree, get_all_child


# 根据反比例函数的定义求参
class HanShuFanBiLi(BaseFunction):
    def solver(self, *args):
        f2 = args[0].expression
        symbol = args[0].var
        target_symbol = f2.free_symbols.difference([symbol])

        # x^(m+1)
        pow_args = get_all_child(f2, lambda x: x.is_Pow and x.args[0].has(symbol) and x.args[1].has(*target_symbol))

        # (m+1)*x**2
        zero_exprs = get_all_child(f2, lambda tt: tt.is_Mul and safe_degree(tt, symbol, -1) != -1)

        # (m+1)*x or (m+1)*x**(m-1)
        nonzero_exprs = get_all_child(f2, lambda tt: tt.is_Mul and safe_degree(tt, symbol, -1) == -1)

        eqs = []
        for arg in pow_args:
            eqs.append([arg.args[1], -S.One])

        for expr in zero_exprs:
            for arg in expr.args:
                if not arg.has(symbol) and arg.has(*target_symbol):
                    eqs.append([arg, S.Zero])

        for expr in nonzero_exprs:
            for arg in expr.args:
                if not arg.has(symbol) and arg.has(*target_symbol):
                    eqs.append([arg, "!=", S.Zero])

        if len(eqs) > 1:
            self.steps.append(["由反比例函数的定义得", self.output_eqs(eqs)])
        else:
            self.steps.append(["由反比例函数的定义得", self.output_eq(eqs[0])])

        self.label.add("根据反比例函数的定义求参")
        self.output.append(base_gen(eqs))
        return self


# 判断反比例函数所在/不在的象限
class HanShuFanBiLiXiangXian(BaseFunction):
    def solver(self, *args):
        f2 = args[0].expression
        symbol = args[0].var
        text = args[1]
        k, h = f2.as_independent(symbol)
        assert len(f2.free_symbols) == 1 and h.exp == -1

        if k > 0:
            self.steps.append(["依题意，得", BaseIneq([k, ">", S.Zero]).printing()])
            jingguoxiangxian = ["一", "三"]
            bujingguoxiangxian = ["二", "四"]
        elif k < 0:
            self.steps.append(["依题意，得", BaseIneq([k, "<", S.Zero]).printing()])
            jingguoxiangxian = ["二", "四"]
            bujingguoxiangxian = ["一", "三"]
        else:
            raise ValueError

        if "不" in text:
            self.steps.append(["", "函数不经过%s象限" % "、".join(bujingguoxiangxian)])
            self.label.add("判断反比例函数不经过的象限")
        else:
            self.steps.append(["", "函数经过%s象限" % "、".join(jingguoxiangxian)])
            self.label.add("判断反比例函数所在象限")
        return self


# 根据反比例函数所在的象限问题求参
class HanShuFanBiLiOnXiangXian(BaseFunction):
    def solver(self, *args):
        f2 = args[0].expression
        symbol = args[0].var
        text = args[1]
        k, h = f2.as_independent(symbol)
        assert h.exp == -1
        if ("一" and "三") in text:
            ineq = [k, ">", 0]
            self.steps.append(["依题意，得", BaseIneq(ineq).printing()])
        elif ("二" and "四") in text:
            ineq = [k, "<", 0]
            self.steps.append(["依题意，得", BaseIneq(ineq).printing()])
        else:
            raise ValueError
        self.output.append(BaseIneq(ineq))
        self.label.add("根据反比例函数所在的象限问题求参")
        return self


if __name__ == '__main__':
    pass
